package com.lagou.module01.servlet;

import com.lagou.module01.bean.ClassGrade;
import com.lagou.module01.service.imp.ClassGradeServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xyk
 * @Date: Create in 0:46 2021/3/20
 * @Description: 查询班级信息的servlet
 */
@WebServlet(name = "QueryClassGradeServlet", urlPatterns = "/queryClassGradeServlet")
public class QueryClassGradeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<ClassGrade> ClassGrades = new ArrayList<>();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

        // service层获取班级信息
        ClassGradeServiceImp ClassGradeServiceImp = new ClassGradeServiceImp();

        String id = request.getParameter("key");

        try {
            ClassGrade ClassGrade = ClassGradeServiceImp.queryClassGradesService(id);
            ClassGrades.add(ClassGrade);
        }
        catch (Exception e){
            out.print(e);
        }
        // 将总页数和所有班级列表放入内置对象中，放入session中
        request.getSession().setAttribute("ClassGradesList", ClassGrades);
        request.getSession().setAttribute("totalPages", 1);
        request.getSession().setAttribute("currentPage", "1");

        // 重定向到班级管理界面
        response.sendRedirect("classManage.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
